﻿using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Domain.ActiveDirectory.Model;

namespace Domain.ActiveDirectory.Mutator
{
    public class GroupMutator : Mutator
    {
        public GroupMutator(Connection connection)
            : base(connection) { }

        public void AddUser(string groupDn, string userDn)
        {
            try
            {
                _connection.Entry.Path = "LDAP://vail.rubios.com/" + groupDn;
                _connection.Entry.Properties["member"].Add(userDn);
                _connection.Entry.CommitChanges();
                _connection.Entry.Close();
            }
            catch (Exception e)
            {
            }
        }

        public void RemoveUser(string groupDn, string userDn)
        {
            try
            {
                _connection.Entry.Path = "LDAP://vail.rubios.com/" + groupDn;
                _connection.Entry.Properties["member"].Remove(userDn);
                _connection.Entry.CommitChanges();
                _connection.Entry.Close();
            }
            catch (Exception e)
            {
            }
        }

        public void MoveUser(string fromGroupDn, string toGroupDn, string userDn)
        {
            RemoveUser(fromGroupDn, userDn);
            AddUser(toGroupDn, userDn);
        }

    }
}
